const express=require('express');
const mysql=require('mysql');
//const common=require('../../libs/common.js');
const urlobj=require('url');
const multer=require('multer');
//const multerObj=multer({dest:'../../www/upload'});
const pathLib=require('path');
const fs=require('fs');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});;
module.exports = function() {
	var router = express.Router();
	//查询首页banner
	router.use('/indexbanner', function(req, res){
		db.query("SELECT * FROM `homepagebanner` WHERE classification =1 ORDER BY `homepagebanner`.`bannerTime` DESC LIMIT 0 , 4",function(err,data){
			if(err)
			{
				res.send('{"status":"400"}').end();
			}
			else
			{
				res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
			}
		});
		
	});

	//查询商品一级分类
	router.use('/indexclassa', function(req, res) {
		db.query("SELECT * FROM `classa`ORDER BY `classa`.`classAtime` DESC",function(err,data){
			if(err)
			{
				res.send('{"status":"0"}').end();
			}
			else
			{
				res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
			}
		});
		
	});

    //查询商品二级级分类
    router.use('/indexclassb', function(req, res) {
        db.query("SELECT * FROM `classb`ORDER BY `classb`.`classBtime` DESC",function(err,data){
            if(err)
            {
                res.send('{"status":"0"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });

    });

    //查询热销商品前六条
    router.use('/indexclassc', function(req, res) {
        db.query("SELECT * FROM `classc` ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 6",function(err,data){
            if(err)
            {
                res.send('{"status":"0"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });

    });

	//根据商品id查询对应的商品
	router.use('/getGoodsInfofor_ClassId', function(req, res) {
		//console.log(req.query);
		var classCId=req.query.classCId;
		db.query("SELECT * FROM `classc` WHERE classCId="+classCId+"",function(err,data){
			if(err)
			{
				res.send('{"status":"0"}').end();
			}
			else
			{
				res.send('{"status":"1","msg":'+JSON.stringify(data)+'}').end();
			}
		});
	});

    //根据二级分类查询对应的商品
    router.use('/getGoodsInfofor_ClassId', function(req, res) {
        db.query("SELECT * FROM classc,classb WHERE classb.classBId=classc.classBId and classb.classBId="+ +req.body.classBId+" ORDER BY `classb`.`classBtime` DESC",function(err,data){
            if(err)
            {
                res.send('{"status":"0"}').end();
            }
            else
            {
                res.send('{"status":"1","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });

    //查询置顶公告前四条
    router.post('/noticeinfoSetTop', function(req, res) {
        db.query("SELECT * FROM `noticeinfo` WHERE activitSetTop =1 ORDER BY `noticeinfo`.`Noticedata` DESC LIMIT 0 , 4",function(err,data){
            if(err)
            {
                res.send('{"status":"0"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });
//查询公告详情
  router.post('/AnnouncementDetails', function(req, res) {
    db.query("SELECT * FROM `noticeinfo` WHERE Noticeid="+req.body.Noticeid+"",function(err,data){
      if(err)
      {
        res.send('{"status":"0"}').end();
      }
      else
      {
        res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
      }
    });
  });
  //查询活动
  router.post('/activityinfoGGG', function(req, res) {
    db.query("SELECT * FROM `activityinfo` ORDER BY `activityinfo`.`activitydata` DESC LIMIT 0 , 10",function(err,data){
      if(err)
      {
        res.send('{"status":"400"}').end();
      }
      else
      {
        res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
      }
    });
  });
    //公告前六条
    router.post('/NoticeFlashback', function(req, res) {
        db.query("SELECT * FROM `noticeinfo` WHERE activitSetTop =0 ORDER BY `noticeinfo`.`Noticedata` DESC LIMIT 0 , 6",function(err,data){
            if(err)
            {
                res.send('{"status":"400"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });

  //查询每一个一级分类的商品  倒叙最新的 数码产品
  router.use('/Electronics', function(req, res) {
    db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =6 ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
      if(err)
      {
        res.send('{"status":"400"}').end();
      }
      else
      {
        res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
      }
    });
  });


  router.use('/Electronicsconto', function(req, res) {
    db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =6 ORDER BY `classc`.`classCTime` DESC",function(err,data){
      if(err)
      {
        res.send('{"status":"400"}').end();
      }
      else
      {
        res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
      }
    });
  });





    //查询每一个一级分类的商品  倒叙最新的 电脑租聘
    router.use('/ComputerLIMITFour', function(req, res) {
      db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =1 ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
        if(err)
            {
                res.send('{"status":"0"}').end();
            }
            else
            {
                res.send('{"status":"1","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });

    router.use('/Computer', function(req, res) {
      db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =1 ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
        if(err)
            {
                res.send('{"status":"0"}').end();
            }
            else
            {
                res.send('{"status":"1","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });

    //查询每一个一级分类的商品  倒叙最新的 生活用品
    router.use('/LifeFlashback', function(req, res) {
      db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =4 ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
        if(err)
            {
                res.send('{"status":"400"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });


    router.use('/LifeFlashbackconto', function(req, res) {
      db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =4 ORDER BY `classc`.`classCTime` DESC",function(err,data){
        if(err)
            {
                res.send('{"status":"400"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });


  //查询每一个一级分类的商品  倒叙最新的 电脑
  router.use('/NotebookComputer', function(req, res) {
    db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =1 ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
      if(err)
      {
        res.send('{"status":"400"}').end();
      }
      else
      {
        res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
      }
    });
  });


  router.use('/NotebookComputerconto', function(req, res) {
    db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =1 ORDER BY `classc`.`classCTime` DESC",function(err,data){
      if(err)
      {
        res.send('{"status":"400"}').end();
      }
      else
      {
        res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
      }
    });
  });


    //查询每一个一级分类的商品  倒叙最新的 	书籍租聘
    router.use('/bookFlashback', function(req, res) {
      db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =3 ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
        if(err)
            {
                res.send('{"status":"400"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });
    router.use('/bookFlashbackconte', function(req, res) {
      db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =3 ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
        if(err)
            {
                res.send('{"status":"400"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    })

    //查询每一个一级分类的商品  倒叙最新的 	租号试玩
    router.use('/Rentnumbertest', function(req, res) {
      db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =2 ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
        if(err)
            {
                res.send('{"status":"400"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });
    router.use('/RentNumberFlashbackconte', function(req, res) {
      db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =2 ORDER BY `classc`.`classCTime` DESC",function(err,data){
        if(err)
            {
                res.send('{"status":"400"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });

  //查询每一个一级分类的商品  倒叙最新的 	玩具
  router.use('/Toyslist', function(req, res) {
    db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =5 ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
      if(err)
      {
        res.send('{"status":"400"}').end();
      }
      else
      {
        res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
      }
    });
  });
  router.use('/Toyslistcont', function(req, res) {
    db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =5 ORDER BY `classc`.`classCTime` DESC",function(err,data){
      if(err)
      {
        res.send('{"status":"400"}').end();
      }
      else
      {
        res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
      }
    });
  });


    //查询每一个一级分类的商品  倒叙最新的 	 	服装
    router.use('/clothingFlashback', function(req, res) {
      db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =8 ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
        if(err)
            {
                res.send('{"status":"400"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });

  router.use('/clothingFlashback', function(req, res) {
    db.query("SELECT * FROM classa, classb, classc WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND classa.classAId =8 ORDER BY `classc`.`classCTime` DESC",function(err,data){
      if(err)
      {
        res.send('{"status":"400"}').end();
      }
      else
      {
        res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
      }
    });
  });

    //查询每一个一级分类的商品  倒叙最新的 	 	 	野外用品
    router.use('/FieldFlashback', function(req, res) {
      db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =7 ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
        if(err)
            {
                res.send('{"status":"400"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });
    router.use('/FieldFlashbackconte', function(req, res) {
      db.query("SELECT * FROM classa, classb, classc,userinfo WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId AND userinfo.userId = classc.userId AND  classa.classAId =7 ORDER BY `classc`.`classCTime` DESC",function(err,data){
        if(err)
            {
                res.send('{"status":"400"}').end();
            }
            else
            {
                res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
            }
        });
    });
//==============================查询销量最火的前四个分类
  router.use('/FireRanking', function(req, res) {
    db.query('SELECT *\n' +
      'FROM `collection` , classc, classb\n' +
      'WHERE collection.classcid = classc.classcid\n' +
      'AND classc.classbid = classb.classbid\n' +
      'GROUP BY classb.classbid DESC\n' +
      'LIMIT 4 ',function (err,data) {
      if (err)
      {
        res.status(500).send('<script>alert("出错了!");window.location.href="404.html";</script>').end();
      }else
      {
        if(data.length==0)
        {
          res.send('{"status":"400"}').end();
        }else
        {

          res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
        }
      }
    });
  });
//查询活动的商品
  router.use('/FieldFlashback', function(req, res) {
    db.query("SELECT * FROM  `classc` WHERE isActive =0 ORDER BY  `classc`.`classCTime` DESC LIMIT 0 , 5",function(err,data){
      if(err)
      {
        res.send('{"status":"300"}').end();
      }
      else
      {
        res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
      }
    });
  });
  //活动商品全部
  router.use('/FieldFlashback', function(req, res) {
    db.query("SELECT * FROM  `classc` WHERE isActive =0 ORDER BY  `classc`.`classCTime` DESC",function(err,data){
      if(err)
      {
        res.send('{"status":"300"}').end();
      }
      else
      {
        res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
      }
    });
  });


//==============================商品的详情 根据id进行查询
  router.post('/CommodityDetails',function (req,res) {
    var commentId= parseInt(req.body.classCId);
    db.query("SELECT * FROM `classc` WHERE classCId="+ commentId+"",function (err,data) {
      if(err)
      {
        res.send('{"status":"400"}').end();
      }else
      {
        if(data.length==0)
        {
          res.send('{"status":"300"}').end();
        }else
        {
          res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
        }
      }
    })

  });

  //==============================商品的id进行查询商品的评论
  router.post('/CommodityReviews',function (req,res) {
    var commentId= parseInt(req.body.classCId);
    db.query("SELECT * FROM commentinfo, userinfo, classc WHERE commentinfo.classCId = classc.classCId AND commentinfo.userId = userinfo.userId and classc.classCId="+ +commentId +" ORDER BY `commentinfo`.`commentTime` DESC",function(err,data){
      if(err)
      {
        res.send('{"status":"400"}').end();
      }else
      {
        if(data.length==0)
        {
          res.send('{"status":"300"}').end();
        }else
        {
          res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
        }
      }
    })
  });
//根据二级分类来查询推荐商品 四条
  router.post('/RecommendationTwo',function (req,res) {
    var classBId= parseInt(req.body.classBId);

    db.query("SELECT *FROM classc, classb WHERE classc.classBId = classb.classBId AND classb.classBId ="+ classBId+" ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 4",function(err,data){
      if(err)
      {
        res.send('{"status":"400"}').end();
      }else
      {
        if(data.length==0)
        {
          res.send('{"status":"300"}').end();

        }else
        {
          res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
        }
      }
    })
  });
  router.use('/selectClassA',function (req,res,next) {
    //创建查询的SQL语句
    db.query('select *from classa',function (err,data) {
      if(err){
        res.send('{"status":"400"}').end();
      }
      else{
        if(data.length==0){
          res.send('{"status":"300"}').end();
        }
        else{
          res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
        }
      }
    })
  });
  router.use('/maxClassB',function (req,res,next) {
    //查询销量最好的前四类商品
    db.query('SELECT *FROM `collection` , classc, ' +
      'classb WHERE collection.classcId = classc.classcId' +
      ' AND classc.classbid = classb.classbid GROUP BY' +
      ' classb.classbid ORDER BY `classb`.`classbid` DESC LIMIT 0 , 4',function (err,data) {
      if(err){
        res.send('{"status":"400"}').end();
      }
      else{
        if(data.length==0){
          res.send('{"status":"300"}').end();
        }
        else{
          res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
        }
      }
    })
  });

  router.use('/selectclassB',function (req,res,next) {
    var classAid=req.body.classAId;

    //查询销量最好的前四类商品
    db.query('select * from classb where classaId='+parseInt(classAid),function (err,data) {
      if(err){
        res.send('{"status":"400"}').end();
      }
      else{
        if(data.length==0){
          res.send('{"status":"300"}').end();
        }
        else{

          res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
        }
      }
    })
  });
  router.use('/activityConnect',function (req,res,next) {
    var Noticeid=req.body.Noticeid;

    //查询销量最好的前四类商品
    db.query('select * from noticeinfo,userinfo where userinfo.userId=noticeinfo.userId and  Noticeid='+parseInt(Noticeid),function (err,data) {
      if(err){
        res.send('{"status":"400"}').end();
      }
      else{
        if(data.length==0){
          res.send('{"status":"300"}').end();
        }
        else{

<<<<<<< HEAD
          res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
        }
      }
    })
  });
=======

  // router.post('/RecommendationTwo',function (req,res) {//查询推荐
  //   var classCId= parseInt(req.body.classCId);
  //   console.log(classCId);
  //   db.query("SELECT *FROM classc, classb WHERE classc.classBId = classb.classBId AND classc.classCId!="+ classCId+" ORDER BY `classc`.`classCTime` DESC LIMIT 0 , 4",function(err,data){
  //     if(err)
  //     {
  //       res.send('{"status":"400"}').end();
  //     }else
  //     {
  //       if(data.length==0)
  //       {
  //         res.send('{"status":"300"}').end();
  //
  //       }else
  //       {
  //         res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
  //       }
  //     }
  //   })
  // });


//==============================商品    查询当前活动商品
//   router.get('/displacritical',function (req,res) {
//     db.query("SELECT * FROM commentinfo, userinfo, classc WHERE commentinfo.classCId = classc.classCId AND commentinfo.userId = userinfo.userId",function (err,data) {
//       if(err)
//       {
//         res.send('{"status":"400"}').end();
//       }else
//       {
//         if(data.length==0)
//         {
//           res.send('{"status":"300"}').end();
//         }else
//         {
//           res.send('{"status":"200","msg":'+JSON.stringify(data)+'}').end();
//         }
//       }
//     })
//
//   });


    
>>>>>>> 721241eaa9cd2ad885f9d14fd13b17ad74e74f72

	return router;
}
